package com.iretailer.report.analyze.device.eas;

import com.alibaba.fastjson.JSON;
import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonUtil.Analyze;
import com.iretailer.report.analyze.commonUtil.ResponeResultType;
import com.iretailer.report.analyze.device.eas.model.AlarmDto;
import com.iretailer.report.util.StringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpContent;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/9/24 0024 13:12
 * @Description:
 */
@AnalyzeClass
@Component
public class EasAnalyze implements Analyze {
    private static final Logger log = LoggerFactory.getLogger(EasAnalyze.class);
    @Value("${netty.eas.alarm}")
    private String easAlarm;
    @Autowired
    private EasInsert easInsert;

    @Override
    public String[] analyzePath() {
        if (StringUtil.notEmpty(easAlarm)) {
            return easAlarm.split(",");
        } else {
            return new String[]{"easalarm"};
        }
    }

    @Override
    public ResponeResultType nettyAnalyze(Map<String, Object> paramsMap, Object msg) {
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf byteBuf = httpContent.content();
            if (byteBuf != null) {
                log.error(byteBuf.toString(CharsetUtil.UTF_8));
                AlarmDto alarmDto = JSON.parseObject(byteBuf.toString(CharsetUtil.UTF_8), AlarmDto.class);
                return easInsert.insertEasAlarm(alarmDto);
            } else {
                log.error("----- ByteBuf is null. {}", paramsMap.get("remoreAddr"));
                return ResponeResultType.getBadRequest(ResponeResultType.CFLType.CLOSE);
            }
        } else {
            return ResponeResultType.getBadRequest(ResponeResultType.CFLType.CLOSE);
        }
    }

    @Override
    public ResponeResultType springAnalyze(Map<String, Object> paramsMap, Object msg) {
        return null;
    }
}
